package com.hxc.shop.controller;

import com.hxc.shop.bean.ShopGoodsComment;
import com.hxc.shop.common.ResultVO;
import com.hxc.shop.service.CommentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/comment")
@CrossOrigin
@Api(tags = {"商品评论数据处理"})
public class CommentController {
    @Resource
    private CommentService commentService;

    @ApiOperation("新增评论")
    @PostMapping("/addComment")
    @Transactional(rollbackFor = Exception.class)
    public ResultVO addComment(@RequestBody ShopGoodsComment comment){
        return commentService.addComment(comment);
    }

    @ApiOperation("根据用户名查找评价记录")
    @GetMapping("/findCommentByUserName")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userName",value = "用户名",required = true,paramType = "String")
    })
    @Transactional(rollbackFor = Exception.class)
    public ResultVO findCommentByUserName(@RequestParam("userName")String userName){
        return commentService.findCommentByUserName(userName);
    }

    @ApiOperation("根据商品编号查找评价记录")
    @GetMapping("/findCommentByGoodsId")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goodsId",value = "商品编号",required = true,paramType = "Integer")
    })
    @Transactional(rollbackFor = Exception.class)
    public ResultVO findCommentByGoodsId(@RequestParam("goodsId")Integer goodsId){
        return commentService.findCommentByGoodsId(goodsId);
    }


    @ApiOperation("根据商品编号查找评价记录")
    @GetMapping("/findCommentByGoodsIdAndUserName")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goodsId",value = "商品编号",required = true,paramType = "Integer"),
            @ApiImplicitParam(name = "userName",value = "用户名",required = true,paramType = "String"),
    })
    @Transactional(rollbackFor = Exception.class)
    public ResultVO findCommentByGoodsIdAndUserName(@RequestParam("goodsId")Integer goodsId,@RequestParam("userName")String userName){
        return commentService.findCommentByGoodsIdAndUserName(goodsId,userName);
    }

    @ApiOperation("根据评论编号删除评价记录")
    @GetMapping("/deleteCommentById")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "评论编号",required = true,paramType = "Integer"),
    })
    @Transactional(rollbackFor = Exception.class)
    public ResultVO deleteCommentById(@RequestParam("id")Integer id){
        return commentService.deleteCommentById(id);
    }

}
